package scau.edu.cn.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import scau.edu.cn.common.utils.ApiResponse;
import scau.edu.cn.entity.JudgeInfoEntity;
import scau.edu.cn.form.CollectForm;
import scau.edu.cn.form.Info;
import scau.edu.cn.repository.JudgeInfoRepository;
import scau.edu.cn.repository.QueryRepository;
import scau.edu.cn.service.JudgeInfoService;
import scau.edu.cn.service.QueryService;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: yuxiang
 * @create: 2020-08-15 12:54
 **/
@Service
public class JudgeInfoServiceImpl implements JudgeInfoService {

    @Autowired
    private JudgeInfoRepository judgeInfoRepository;

    @Autowired
    private QueryRepository queryRepository;

    @Override
    public ApiResponse saveList(CollectForm form) {
        ApiResponse apiResponse = new ApiResponse();
        try {
            List<JudgeInfoEntity> entities = new ArrayList<>();
//            String queryId = form.getQueryId();
            String type = form.getType();
            if (StringUtils.isBlank(type)&&StringUtils.isEmpty(type)){
                return new ApiResponse(-1,"类别为空");
            }
            if (form.getInfos().size() == 0){
                return new ApiResponse(-1,"信息列表为空");
            }
            for (Info info : form.getInfos()) {
                entities.add(new JudgeInfoEntity(type,info.getName(), info.getScore()));
            }
            //保存列表
            judgeInfoRepository.saveAll(entities);
            //更改问卷状态
//            queryRepository.updateFlagById(queryId,1);

        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(-1, "服务器错误");
        }
        apiResponse.setMsg("保存成功");
        return apiResponse;
    }

    @Override
    public ApiResponse saveList(List<Info> infos) {
        ApiResponse apiResponse = new ApiResponse();
        try {
            List<JudgeInfoEntity> entities = new ArrayList<>();

            if (infos.size() == 0){
                return new ApiResponse(-1,"信息列表为空");
            }
            for (Info info : infos) {
                entities.add(new JudgeInfoEntity(info.getTypeName(),info.getName(), info.getScore()));
            }
            //保存列表
            judgeInfoRepository.saveAll(entities);

        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(-1, "服务器错误");
        }
        apiResponse.setMsg("保存成功");
        return apiResponse;
    }
}
